package com.example.zs.appbarlayouttest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zs.appbarlayouttest.event.RecyclerViewEvent;
import com.example.zs.appbarlayouttest.event.SwipeEnableEvent;
import com.example.zs.appbarlayouttest.listener.OnCurrentRvListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecyclerViewFragment extends Fragment implements OnCurrentRvListener {

    private RecyclerView mRecyclerView;

    /**
     * handler
     **/
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:

                    break;
                case 1:

                    break;

                }
            super.handleMessage(msg);
        }
    };

    public static RecyclerViewFragment newInstance() {
        RecyclerViewFragment recyclerViewFragment = new RecyclerViewFragment();
        return recyclerViewFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recyclerview, container, false);
        List<String> strlist = new ArrayList<String>();
        for (int i = 0; i < new Random().nextInt(100) + 31; i++) {
            strlist.add(String.valueOf(i));
        }
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(new RecyclerAdapter(getActivity(), strlist));
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
                if (manager instanceof LinearLayoutManager){

                    View view = recyclerView.getChildAt(0);
                    if (view == null || manager.getDecoratedTop(view) == 0){
                        EventBus.getDefault().post(new RecyclerViewEvent(true));
                    }
                }
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("My_Fragment", "method = " + "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("My_Fragment", "method = " + "onPause");
    }

    @Override
    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    public void updateData(){
        EventBus.getDefault().post(new SwipeEnableEvent(true));
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                EventBus.getDefault().post(new SwipeEnableEvent(false));
            }
        },1000);
    }
}
